<!-- 钱包绑定地址页面 -->
<template>
  <div id="walletSiteBind">
    <c-title :hide="false" text="钱包地址"></c-title>
    <van-cell-group inset style="margin-top: 1rem;">
      <van-form @submit="onSubmit">
        <van-field v-model="site" name="钱包地址" label="钱包地址" placeholder="请输入钱包地址" :rules="[{ required: true}]" />
        <div style="margin: 16px;">
          <van-button round block type="info" native-type="submit" color="#f15353">提交</van-button>
        </div>
      </van-form>
    </van-cell-group>

    <!-- 密码弹窗组件 -->
    <pay-keyboard :payKeyboardShow.sync="payKeyboardShow" @complete="payComplete"></pay-keyboard>
  </div>
</template>

<script>
import payKeyboard from "components/payKeyboard";
import { payKeyboardAsync } from "utils/payKeyboard";
export default {
  data() {
    return {
      site: "",
      payKeyboardShow: false,
      need_password: false,
      has_password: false
    };
  },

  activated() {
    this.getData();
  },

  components: { payKeyboard },

  computed: {},

  mounted() {},

  methods: {
    async getData() {
      let {
        result,
        msg,
        data: { site, need_password, has_password}
      } = await $http.get("plugin.love-speed-pool.frontend.wallet-site.index");
      if (result == 0) return this.$toast(msg);
      this.site = site;
      this.need_password = need_password;
      this.has_password = has_password;
    },
    async onSubmit() {
      let json = {site: this.site};
      if (this.need_password) {
        if(!this.has_password) return this.$router.push(this.fun.getUrl("set_balance_password", {}));// 用户未设置支付密码，去设置密码
        let pass = await this.getPayKeyboardPassword();//弹出输入密码框
        json.password = pass;
      }
      let { result, msg } = await $http.get("plugin.love-speed-pool.frontend.wallet-site.edit-info",json);
      if (result == 0) return this.$toast(msg);
      this.$router.go(-1);
    },
    payComplete(text) {
      payKeyboardAsync.complete(text);
    },
    getPayKeyboardPassword() {
      this.payKeyboardShow = true;
      return new Promise((resove, reject) => {
        payKeyboardAsync.addCompleteFn(pass => {
          $http.get("payment.password.check", { password: pass }, "").then(
            (response) => {
              if (response.result == 1) {
                resove(pass);
              } else {
                this.$dialog.alert({ message: response.msg });
              }
            });
        });
      });
    },
  }
};
</script>
<style lang="scss" rel="stylesheet/scss" scoped></style>
